/*
 * @(#)Thumbnail.java 0.0.1-SNAPSHOT
 *
 * Copyright Group B
 */

package grupb.upf.imageProcessing;

import java.awt.Image;
import java.io.File;
import java.io.Serializable;
import javax.swing.ImageIcon;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>This class implement the Thumbnail interface. It allows us to create a
 * thumbnail and returns it into byte because it's necessary to send it by the
 * network. As a result, it has to be serialized and we need to send it as a
 * bytes array. Then, we are able to recover the thumbnail to an ImageIcon.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class Thumbnail implements Serializable {

    /**
     * The logger. A new logger <code>grupb.upf.ms.impl.Thumbnail</code> is
     * created to add the logs of this Thumbnail.
     */
    private static final Log LOG = LogFactory.getLog(Thumbnail.class);
    /**
     * The serialVersionUID is created to be able to serialize this class, it's
     * necessary because it will be sent.
     */
    private static final long serialVersionUID = 1L;
    /**
     * Max width of the thumbnail.
     */
    private static final int MAX_WIDTH = 250;
    /**
     * Maximum height of the thumbnail.
     */
    private static final int MAX_HEIGHT = 200;

    /**
     * This method allows us to create a Thumbnail to be sent by te network.
     * @param originalURL URL of the image, which can be relative or absolute
     * @return an ImageIcon scaled to the default values
     */
    public static ImageIcon getThumbnail(final String originalURL) {
           return getThumbnail(originalURL, MAX_WIDTH, MAX_HEIGHT);
    }
    /**
     * This method allows us to create a Thumbnail to be sent by te network.
     * @param originalURL URL of the image, which can be relative or absolute
     * @param width the width of the image to return
     * @param height the height of the image to return
     * @return array of bytes serialized and prepared to be sent by te network
     */
    public static ImageIcon getThumbnail(final String originalURL,
            final int width, final int height) {
        ImageIcon iconThumb = null;
        ImageIcon image = null;
        final File file = new File(originalURL);
        if (file.exists()) {
            image = new ImageIcon(originalURL);
        } else {
            if (LOG.isErrorEnabled()) {
                StringBuffer sb = new StringBuffer(
                    "Error, couldn't read the image: ")
                    .append(originalURL);
                LOG.error(sb);
            }
            return null;
        }
        final int wImage = image.getIconWidth();
        final int hImage = image.getIconHeight();
        //We calculate the scale
        int w = 0;
        int h = 0;
        final double scaleX = (double) width / (double) wImage;
        final double scaleY = (double) height / (double) hImage;
        final double scale = Math.min(scaleX, scaleY);
        if (scale > 1) {
            w = wImage;
            h = hImage;
        } else {
            w = (int) (scale * wImage);
            h = (int) (scale * hImage);
        }
        iconThumb = new ImageIcon(image.getImage()
                .getScaledInstance(w, h, Image.SCALE_FAST));
       return iconThumb;
    }
}
